package com.draven.admin.controller;

import com.draven.common.core.domain.AjaxResult;
import com.draven.common.entity.NoteTag;
import com.draven.system.service.INoteTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 笔记标签Controller
 * 
 * @author draven
 */
@Controller
@RequestMapping("/note/tag")
public class NoteTagController {

    private String prefix = "tag";

    @Autowired
    private INoteTagService noteTagService;

    @GetMapping()
    public String tag() {
        return prefix + "/tag";
    }

    /**
     * 查询标签列表
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(NoteTag tag) {
        List<NoteTag> list = noteTagService.selectTagList(tag);
        return AjaxResult.success(list);
    }

    /**
     * 查询标签列表（包含使用次数）
     */
    @GetMapping("/listWithCount")
    @ResponseBody
    public AjaxResult listWithCount() {
        List<NoteTag> list = noteTagService.selectTagListWithCount();
        return AjaxResult.success(list);
    }

    /**
     * 查询所有标签列表 - 用于前端多选功能
     */
    @GetMapping("/listAll")
    @ResponseBody
    public AjaxResult listAll() {
        NoteTag tag = new NoteTag();
        List<NoteTag> list = noteTagService.selectTagList(tag);
        return AjaxResult.success(list);
    }

    /**
     * 查询热门标签
     */
    @GetMapping("/hotTags")
    @ResponseBody
    public AjaxResult hotTags(@RequestParam(defaultValue = "10") Integer limit) {
        List<NoteTag> list = noteTagService.selectHotTags(limit);
        return AjaxResult.success(list);
    }

    /**
     * 新增标签
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存标签
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated @RequestBody NoteTag tag) {
        if (!noteTagService.checkTagNameUnique(tag)) {
            return AjaxResult.error("新增标签'" + tag.getTagName() + "'失败，标签名称已存在");
        }
        return AjaxResult.success(noteTagService.insertTag(tag));
    }

    /**
     * 修改标签
     */
    @GetMapping("/edit/{tagId}")
    public String edit(@PathVariable("tagId") Long tagId, ModelMap mmap) {
        NoteTag tag = noteTagService.selectTagById(tagId);
        mmap.put("tag", tag);
        return prefix + "/edit";
    }

    /**
     * 修改保存标签
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated @RequestBody NoteTag tag) {
        if (!noteTagService.checkTagNameUnique(tag)) {
            return AjaxResult.error("修改标签'" + tag.getTagName() + "'失败，标签名称已存在");
        }
        return AjaxResult.success(noteTagService.updateTag(tag));
    }

    /**
     * 删除标签
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return AjaxResult.success(noteTagService.deleteTagByIds(convertToLongArray(ids)));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 校验标签名称
     */
    @PostMapping("/checkTagNameUnique")
    @ResponseBody
    public boolean checkTagNameUnique(NoteTag tag) {
        return noteTagService.checkTagNameUnique(tag);
    }

    /**
     * 转换字符串数组为Long数组
     */
    private Long[] convertToLongArray(String str) {
        String[] arr = str.split(",");
        Long[] longArr = new Long[arr.length];
        for (int i = 0; i < arr.length; i++) {
            longArr[i] = Long.parseLong(arr[i]);
        }
        return longArr;
    }
}